{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 生成一个由八种颜色随机组成的10万个元素组成的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "collist = [random.choice([\"red\",\"blue\",\"green\",\n",
    "                          \"yellow\",\"black\",\"gray\",\n",
    "                         \"white\",\"orange\"])\\\n",
    "           for i in range(100000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['black', 'white', 'white', 'blue', 'yellow', 'blue', 'white', 'red', 'blue', 'green', 'white', 'white', 'red', 'orange', 'gray', 'blue', 'red', 'yellow', 'gray', 'red', 'blue', 'yellow', 'gray', 'red', 'red', 'yellow', 'gray', 'green', 'yellow', 'yellow', 'white', 'red', 'gray', 'orange', 'blue', 'red', 'green', 'black', 'orange', 'orange', 'red', 'green', 'blue', 'red', 'blue', 'black', 'green', 'yellow', 'white', 'black', 'orange', 'green', 'white', 'red', 'orange', 'green', 'green', 'white', 'yellow', 'red', 'orange', 'gray', 'black', 'red', 'red', 'blue', 'orange', 'white', 'green', 'yellow', 'black', 'yellow', 'yellow', 'gray', 'white', 'red', 'red', 'white', 'green', 'white', 'gray', 'red', 'black', 'white', 'blue', 'black', 'orange', 'orange', 'blue', 'orange', 'orange', 'green', 'orange', 'black', 'red', 'white', 'black', 'yellow', 'red', 'red']\n"
     ]
    }
   ],
   "source": [
    "print(collist[:100])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyspark"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = pyspark.SparkContext()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "colrdd = sc.parallelize(collist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['black', 'white', 'white', 'blue', 'yellow', 'blue', 'white', 'red', 'blue', 'green', 'white', 'white', 'red', 'orange', 'gray', 'blue', 'red', 'yellow', 'gray', 'red', 'blue', 'yellow', 'gray', 'red', 'red', 'yellow', 'gray', 'green', 'yellow', 'yellow', 'white', 'red', 'gray', 'orange', 'blue', 'red', 'green', 'black', 'orange', 'orange', 'red', 'green', 'blue', 'red', 'blue', 'black', 'green', 'yellow', 'white', 'black', 'orange', 'green', 'white', 'red', 'orange', 'green', 'green', 'white', 'yellow', 'red', 'orange', 'gray', 'black', 'red', 'red', 'blue', 'orange', 'white', 'green', 'yellow', 'black', 'yellow', 'yellow', 'gray', 'white', 'red', 'red', 'white', 'green', 'white', 'gray', 'red', 'black', 'white', 'blue', 'black', 'orange', 'orange', 'blue', 'orange', 'orange', 'green', 'orange', 'black', 'red', 'white', 'black', 'yellow', 'red', 'red']\n"
     ]
    }
   ],
   "source": [
    "print(colrdd.take(100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 定义个判断方法，如果等于red，就返回True，否则就返回False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isRed(col):\n",
    "    if col == \"red\":\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 执行过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "redrdd = colrdd.filter(lambda col:isRed(col))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red']\n"
     ]
    }
   ],
   "source": [
    "print(redrdd.take(100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 十万条数据中，为red的数据有多少"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12487"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "redrdd.count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 上面那个判断方法，也可以非常简化的写成："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12487"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colrdd.filter(lambda col : col == \"red\").count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 看看等于红色，或者等于蓝色的有多少个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24810"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colrdd.filter(lambda col : col == \"red\" or col == \"blue\").count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
